//import java.util.LinkedList;

/**
 * Created by ospip on 2017/9/25.
 * Copyright (c) 2017 LLAP. All rights reserved.
 */
public class TreeNode<T extends Comparable<? super T>> {

    public T data;
    TreeNode<T> sibling, parent, children;

    public TreeNode(T data, TreeNode<T> parent) {
        this.data = data;
        this.parent = parent;
        this.sibling = null;
        this.children = null;
    }

    public TreeNode(TreeNode<T> t) {
        this.data = t.data;
        this.parent = t.parent;
        this.sibling = t.sibling;
        this.children = t.children;
    }

    public TreeNode(T data) {
        this.data = data;
    }

    public TreeNode() {
        this(null, null);
    }

    @Override
    public String toString() {
        return data.toString();
    }

    public void addChild(T child) {
        TreeNode<T> t = new TreeNode<>(child, this);
        if (this.children == null) {
            this.children = t;
        }
        else {
            t.sibling = this.children;
            this.children = t;
        }
    }

    public void addSibling(T sib) {
        TreeNode<T> t = new TreeNode<>(sib, this.parent);
        if (this.sibling == null) {
            this.sibling = t;
        }
        else {
            t.sibling = this.sibling;
            this.sibling = t;
        }
    }

    public void setParent(TreeNode<T> p) {
        this.parent = p;
        if (p.children == null) p.children = this;
        else {
            this.sibling = p.children;
            p.children = this;
        }
    }

}
